<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 队列
    // function queue(num) {
    //   let promise = Promise.resolve()
    //   num.map(v => {
    //     promise = promise.then(_=> {
    //       return new Promise(resolve => {
    //         setTimeout(() => {
    //           console.log(v)
    //           resolve()
    //         }, 1000)
    //       })
    //     })
    //   })
    // }
    // queue([1,2,3,4,5])

    function queue(num) {
      let promise = Promise.resolve();
      num.map(v => {
        promise = promise.then(_ => {
          return v()
        })
      })
    }
    function p1() {
      return new Promise(resolve => {
        setTimeout(() => {
          console.log('p1')
          resolve()
        }, 1000)
      })
    }
    function p2() {
      return new Promise(resolve => {
        setTimeout(() => {
          console.log('p2')
          resolve()
        }, 1000)
      })
    }
    queue([p1, p2])
  </script>
</body>
</html>